package com.hdax.dm.controller;

import com.hdax.dm.dto.FlootDto;
import com.hdax.dm.dto.IndexNavDto;
import com.hdax.dm.dto.ItemDto;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.entity.item.DmItemType;
import com.hdax.dm.exception.DmException;
import com.hdax.dm.service.ItemService;
import com.hdax.dm.service.ItemTypeService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/item/index")
public class IndexController {

    @Autowired
    private ItemTypeService itemTypeService;

    @Autowired
    private ItemService itemService;

    /*
    * 首页导航数据查询显示（一级导航、二级导航、热门商品(5)）
    * */
    @PostMapping("nav")
    public CommonResponse<List<IndexNavDto>> nav(){
        return itemTypeService.navs();
    }

    // 获取首页横向导航
    @PostMapping("lineNav")
    public CommonResponse<List<DmItemType>> lineNav(){
        return itemTypeService.lineNav();
    }


    // 获取今日推荐
    @PostMapping("recommend")
    public CommonResponse<List<ItemDto>> recommend(){
        return itemService.isRecommend();
    }

    // 获取即将开售
    @PostMapping("sell")
    public CommonResponse<List<ItemDto>> sell(){
        return itemService.sell();
    }

    // 获取楼层数据
    @PostMapping("floor")
    public CommonResponse<List<FlootDto>> floorData(){
        return itemService.floorData();
    }

    // 获取热门排行榜
    @PostMapping("seniority")
    public CommonResponse<List<DmItem>> seniority(@RequestBody Map<String,Long> map){
        return itemService.seniority(map.get("itemTypeId"));
    }

    //凡是本类中的方法抛出相关异常，都会进入该方法统一处理
    @ExceptionHandler
    public CommonResponse<String> exceptionHandler(DmException e){
        return ResponseUtil.returnFail(e);
    }
}
